package com.beiding.fastmock.fram;

import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import io.netty.handler.codec.compression.Brotli;
import org.apache.commons.compress.compressors.brotli.BrotliCompressorInputStream;
import org.apache.commons.compress.compressors.brotli.BrotliUtils;
import org.brotli.dec.BrotliInputStream;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public abstract class HttpBase {


    private MultiValueMap<String, String> headers;

    private Object jsonBody;

    private String strBody;

    private byte[] body;

    public byte[] getBody() {
        return body;
    }


    public String getStrBody() {
        if (strBody != null) {
            return strBody;
        }
        if (getBody() == null) {
            return null;
        }
        return strBody = new String(getBody());
    }

    public Object getJsonBody() {
        if (jsonBody != null) {
            return jsonBody;
        }
        return jsonBody = JSON.parse(getStrBody());
    }


    public boolean isHasBody() {
        return getBody() != null;
    }


    public void setJsonBody(Object jsonBody) {
        this.jsonBody = jsonBody;
    }

    public void setStrBody(String strBody) {
        this.strBody = strBody;
    }


    public void setBody(byte[] body) {
        this.body = body;
    }

    public List<String> removeHeader(String key) {
        return getHeader().remove(key);
    }

    public boolean removeHeader(String key, Object value) {
        return getHeader().remove(key, value);
    }


    public List<String> getHeader(String key) {
        return getHeader().get(key);
    }

    public String getFirstHeader(String key) {
        return getHeader().getFirst(key);
    }


    public void setHeader(String key, String value) {
        getHeader().set(key, value);
    }


    public MultiValueMap<String, String> getHeader() {
        return headers;
    }

    public void setHeader(MultiValueMap<String, String> headers) {
        this.headers = headers;
    }


    //解码内容
    public void unzip() {
        String firstHeader = getFirstHeader("Content-Encoding");
        if (firstHeader != null) {
            try {
                if ("br".equals(firstHeader)) {
                    BrotliInputStream brotliInputStream = new BrotliInputStream(new ByteArrayInputStream(getBody()));
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    StreamUtils.copy(brotliInputStream, byteArrayOutputStream);
                    setBody(byteArrayOutputStream.toByteArray());
                    removeHeader("Content-Encoding");
                } else if ("gzip".equals(firstHeader)) {
                    GZIPInputStream brotliInputStream = new GZIPInputStream(new ByteArrayInputStream(getBody()));
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    StreamUtils.copy(brotliInputStream, byteArrayOutputStream);
                    setBody(byteArrayOutputStream.toByteArray());
                    removeHeader("Content-Encoding");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
